package org.yate.oa.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.yate.oa.model.HighVoltage;
import org.yate.oa.service.HighVoltageService;

import com.alibaba.fastjson.JSONObject;

@SuppressWarnings("serial")
@WebServlet(urlPatterns = { "/highVolCabinet/get", "/highVolCabinet/post", "/highVolCabinet/put", "/highVolCabinet/delete" }, asyncSupported = true)
public class HighVolCabinetController extends BaseServletController
{
    private HighVoltageService highVoltageService;

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        this.highVoltageService = this.springCtx.getBean(HighVoltageService.class);
    }
    
    protected void doDelete(HttpServletRequest req, HttpServletResponse rsp) throws ServletException, IOException {
        String id = req.getParameter("id");
        PrintWriter out = rsp.getWriter();
        JSONObject json = new JSONObject();
        highVoltageService.remove(Long.parseLong(id));
        json.put("code", "200");
        json.put("msg", "ok");
        out.println(json);
        out.flush();
        out.close();
    }
    
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        PrintWriter out = resp.getWriter();
        JSONObject json = new JSONObject();
        HighVoltage e = highVoltageService.getEntity(Long.parseLong(id));
        json.put("e", e);
        out.println(json);
        out.flush();
        out.close();
    }
    
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        String[] kva = req.getParameterValues("kva");
        String highVol = req.getParameter("highVol");
        String lowVol = req.getParameter("lowVol");
        String boxNum = req.getParameter("boxNum");
        String highVolConf = req.getParameter("highVolConf");
        String voltageConf = req.getParameter("voltageConf");
        String submitType = req.getParameter("submitType");
        String highVolBoxConf = req.getParameter("highVolBoxConf");
        String brand = req.getParameter("brand");
        String operationCode = req.getParameter("operationCode");
        HighVoltage e = new HighVoltage();
        e.setType(1);
        e.setKva(StringUtils.join(kva, ","));
        e.setHighVol(Integer.parseInt(highVol));
        e.setLowVol(Integer.parseInt(lowVol));
        e.setBoxNum(Integer.parseInt(boxNum));
        e.setHighVolConf(Integer.parseInt(highVolConf));
        e.setVoltageConf(Integer.parseInt(voltageConf));
        e.setHighVolBoxConf(Integer.parseInt(highVolBoxConf));
        e.setBrand(Integer.parseInt(brand));
        e.setOperationCode(Integer.parseInt(operationCode));
        String detail = req.getParameter("detail");
        e.setDetail(detail);
        if(submitType.equals("add")) {
            highVoltageService.add(e);
        } else {
            String id = req.getParameter("id");
            e.setId(Long.parseLong(id));
            highVoltageService.update(e);
        }
      
        req.getRequestDispatcher("/jsp/highVolCabinet/list.jsp").forward(req, resp);
    }
}
